<?php
/**
 * Create By: PhpStorm
 * User: yxc
 * File: UserAccount .php
 * Date: 2017/1/6
 * Time: 0:00
 */
namespace app\api\model\page\user;

use app\api\model\dao\UserAccount;
use app\api\model\dao\UserCurrency;
use app\library\Common;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Validate;

class GetUserCommunityLevel extends PageBase
{

    private $objUserAccountModel;
    private $objUserCurrencyModel;

    public function __construct()
    {
        parent::__construct();
        $this->objUserAccountModel = new UserAccount();
        $this->objUserCurrencyModel = new UserCurrency();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'token' => 'require',
        ];
        $msg = [
            'token.require' => 'token必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {

        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }

        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $user_account = $this->objUserAccountModel->getUserAccount(['user_id' => $this->user_id]);
        if (false === $user_account) {
            return $this->getRes(Error::ERR_SYS);
        }
        if (empty($user_account)) {
            return $this->getRes(Error::ERR_USER_NOT_EXIST);
        }
        $user_account = $user_account[0];

        $user_currency = $this->objUserCurrencyModel->getCurrencyByUserid($this->user_id, Flag::CURENCY_TYPE_INTEGRAL);
        if (false === $user_currency) {
            return $this->getRes(Error::ERR_SYS);
        }

        $level = Common::conversionLevel($user_currency['amount']);

        $this->data = [
            'user' => [
                'photo' => $user_account['avatar'],
                'nick_name' => $user_account['nick_name'],
                'level' => $level,
                'total_level' => 100,
            ]
        ];

        return $this->getRes();
    }
}


